package com.brooks.demo;

/**
 * 单例模式 加锁
 * 锁粒度变细，但线程不安全
 * @Author: mihutu
 * @Date: 2020/10/11 21:47
 */

public class DemoSingleton4 {
    private static DemoSingleton4 singleton ;

    private DemoSingleton4() {

    }

    public static DemoSingleton4 getInstance() {
        if (singleton == null){
            synchronized (DemoSingleton4.class) {
                singleton = new DemoSingleton4();
            }
        }
        return singleton;
    }

    public static void main(String[] args) {
        for (int i=0;i<100;i++){
            new Thread(()->{
                System.out.println(getInstance().hashCode());
            }).start();
        }
    }
}
